#include<iostream>
#include<string>
#include"comm.hpp"
#include"mysql.h"

bool check(MYSQL* conn, std::string thing,int t)
{
  std::string s;
    if(t==0)
     s="select * from user where name=\'";
    else 
     s="select * from user where password=\'";
    
    s+=thing;
    s+="\'";
    std::cerr<<s<<std::endl;
    mysql_query(conn,s.c_str());
	MYSQL_RES *mr=mysql_use_result(conn);
    //打印查询结果
    MYSQL_ROW row;
		unsigned int cols = mysql_field_count(conn);
    int flag=0;
while((row=mysql_fetch_row(mr)))
    {
      flag=1;
			for(unsigned int i=0;i<cols;i++)
			{
				std::cerr<<row[i];
			}
			std::cerr<<std::endl;
    }
  if(flag==1)
  {
    return true;
  }
  else 
  {
    return false;
  }
}
int main()
{
   std::string query_string;
    //from http
    if(GetQueryString(query_string)){
        std::cerr << query_string << std::endl;
        //数据处理？
        //name=tom&passwd=111111

        std::string name;
        std::string passwd;

        CutString(query_string, "&", name, passwd);

        std::string _name;
        std::string sql_name;
        CutString(name, "=", _name, sql_name);

        std::string _passwd;
        std::string sql_passwd;
        CutString(passwd, "=", _passwd, sql_passwd);
 
       MYSQL *conn = mysql_init(nullptr);
        mysql_set_character_set(conn, "utf8");
        if(nullptr == mysql_real_connect(conn, "127.0.0.1", "http", "shuguang", "http_test", 3306, nullptr, 0))
        {
        std::cerr << "connect error!" << std::endl;
        return 1;
        }
        if(sql_name.size()==0)
        {
          std::cout << "<html>";
          std::cout << "<head><meta charset=\"utf-8\"></head>";
          std::cout << "<body><h1>请输入账号</h1>";
          std::cout<<"<a href=\"http://101.35.110.87:8081\">返回</a></body></html>";
          return 0;
        }
        if(sql_passwd.size()==0)
        {
          std::cout << "<html>";
          std::cout << "<head><meta charset=\"utf-8\"></head>";
          std::cout << "<body><h1>请输入密码</h1>";
          std::cout<<"<a href=\"http://101.35.110.87:8081\">返回</a></body></html>";
          return 0;
        }
        if(!check(conn,sql_name,0))
        {
          std::cerr<<"用户不存在"<<std::endl;
          std::cout << "<html>";
          std::cout << "<head><meta charset=\"utf-8\"></head>";
          std::cout << "<body><h1>该用户不存在存在，请重新登录</h1>";
          std::cout<<"<a href=\"http://101.35.110.87:8081\">返回</a></body></html>";
          return 0;
        }
        if(!check(conn,sql_passwd,1))
        {
          std::cerr<<"密码错误"<<std::endl;
          std::cout << "<html>";
          std::cout << "<head><meta charset=\"utf-8\"></head>";
          std::cout << "<body><h1>密码输入错误，请重新输入</h1>";
          std::cout<<"<a href=\"http://101.35.110.87:8081\">返回</a></body></html>";
          return 0;
        }
        std::cout<<"<head><meta charset = \"utf-8\"><title>计算器</title></head>";
        std::cout<<"<body><form action=\"/calculate_cgi\"method=\"POST\"> first num: <br><input type=\"text\" name=\"a\"><br>operation:+-*/<br><input type=\"text\" name=\"c\"><br>second num:<br><input type=\"text\" name=\"b\"><br><input type=\"submit\" value=\"提交\"><br><a href=\"https://blog.csdn.net/hbbfvv1h?spm=1000.2115.3001.5343\">博客链接</a><br><a href=\"https://gitee.com/teng-zhengcai\">码云链接</a></body></html>"
 ;   }

    else 
    {
      std::cerr<<"获取字符串失败"<<std::endl;
    }
        return 0;
}
